package com.ve.clexer.tree;

import com.ve.clexer.CLanguage;

import java.util.HashMap;

public class TreeBuilder {
    public Node last;
    public RangeNode area;
    public HashMap<String, BasicType> basicTypes = new HashMap<>();

    private void initBasicType() {
        String[] names = CLanguage.BASIC_TYPE_NAMES;
        for (int i = 0; i < names.length; i++) {
            basicTypes.put(names[i], new BasicType(names[i]));
        }
    }

    public BasicType findBaseType(String name) {
        return basicTypes.get(name);
    }

    public TreeBuilder() {
        initBasicType();
    }


    public Node lastNode() {
        return last;
    }

    public Type findType(String name) {
        Type t = findBaseType(name);
        if (t != null) {
            return t;
        }

        for (CustomType type : area.types) {
            if (type.getName().equals(name)) {
                return type;
            }
        }


        return null;
    }

    public void addFunction(Function fun) {
        area.functions.add(fun);
    }


    public RangeNode getArea() {
        return area;
    }

    public void seek(RangeNode node) {
        area = node;
        last = node;
    }
}
